package jp.co.realsys.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import jp.co.realsys.dao.StudentDao;
import jp.co.realsys.error.TaskException;
import jp.co.realsys.model.StudentModel;

/**
 * 学生情報ファイルのインタフェースの実装クラス
 * 
 * @author
 */
@Repository
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao {

	@Resource
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	
	@Override
	public int doAddStduent(StudentModel student) throws TaskException {
		int result = getSqlSession().insert("jp.co.realsys.dao.impl.studentMapper.add", student);
		return result;
	}

	@Override
	public int doUpdateStduent(StudentModel student) throws TaskException {
		int result = getSqlSession().update("jp.co.realsys.dao.impl.studentMapper.update", student);
		return result;
	}

	@Override
	public int doDeleteStduent(String studentId) throws TaskException {
		int result = getSqlSession().delete("jp.co.realsys.dao.impl.studentMapper.delete", studentId);
		return result;
	}

	@Override
	public List<StudentModel> doSelectStduentList(String studentName)
			throws TaskException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StudentModel stu = new StudentModel();
		stu.setName(studentName);
		paramMap.put("student", stu);
		paramMap.put("orderBy", "name, birthday desc, id desc");
		List<StudentModel> stuList = getSqlSession().selectList("jp.co.realsys.dao.impl.studentMapper.select", paramMap);
		return stuList;
	}

	@Override
	public List<StudentModel> getAllStudents() throws TaskException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderBy", "name, birthday desc, id desc");
		List<StudentModel> stuList = getSqlSession().selectList("jp.co.realsys.dao.impl.studentMapper.getAllStudent", paramMap);
		return stuList;
	}

	@Override
	public StudentModel getStudentById(String id) throws TaskException {
		StudentModel stu = getSqlSession().selectOne("jp.co.realsys.dao.impl.studentMapper.getStudentById", id);
		return stu;
	}
	
}
